<?php
/**
 * 数据库更安全，SQL执行更高效
 * 
 * @package DBpretty
 * @author WiFeng
 * @version 1.0.0
 * @link http://521-wf.com
 */

class DBpretty_Plugin implements Typecho_Plugin_Interface
{

    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     * 
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate() {
    	$adapterName = Typecho_Db::get()->getAdapterName();
    	if(strtoupper($adapterName) != 'MYSQL') {
    		throw new Typecho_Plugin_Exception(_t('该插件只支持Mysql作为数据库的站点哦，你当前的数据库类型是：' . $adapterName));
    	}
    	if(!function_exists('shell_exec')) {
    		throw new Typecho_Plugin_Exception(_t('该插件需要 shell_exec() 这个函数的支持，您当前的php环境没有此函数'));
    	}
    	
		$menuid = 3;
		Helper::addPanel($menuid, 'DBpretty/manage.php', _t('数据库'), _t('数据库'), 'administrator');
		Helper::addAction('dbpretty-do', 'DBpretty_Do');
    }
    
    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     * 
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate(){
    	$menuid = 3;
    	Helper::removePanel($menuid, 'DBpretty/manage.php');
    	Helper::removeAction('dbpretty-do');
	}
    
    /**
     * 获取插件配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form) {
    	$dbName = new Typecho_Widget_Helper_Form_Element_Text('dbname', NULL, '', _t('数据库名称'));
    	$dbHost = new Typecho_Widget_Helper_Form_Element_Text('dbhost', NULL, 'localhost', _t('数据库地址'));
    	$dbUser = new Typecho_Widget_Helper_Form_Element_Text('dbuser', NULL, '', _t('数据库用户名'));
    	$dbPass = new Typecho_Widget_Helper_Form_Element_Password('dbpass', NULL, '', _t('数据库密码'));
    	$dbCharset = new Typecho_Widget_Helper_Form_Element_Text('dbcharset', NULL, '', _t('数据库字符集'), _t('如果是UTF-8，就直接输入：utf8'));
    	$dbPort = new Typecho_Widget_Helper_Form_Element_Text('dbport', NULL, '3306', _t('数据库端口号'));
    	$backupDir = new Typecho_Widget_Helper_Form_Element_Text('backupdir', NULL, 'backup_'.DBpretty_Options::random(6), _t('数据备份目录'), _t('填写目录将位于 usr/下，不能是plugins、themms 、uploads这些系统目录'));
    	
    	$dbName->input->setAttribute('style', 'width:300px');
    	$dbHost->input->setAttribute('style', 'width:300px');
    	$dbUser->input->setAttribute('style', 'width:300px');
    	$dbPass->input->setAttribute('style', 'width:300px');
    	$dbCharset->input->setAttribute('style', 'width:300px');
    	$dbPort->input->setAttribute('style', 'width:300px');
    	$backupDir->input->setAttribute('style', 'width:300px');
    	
    	$form->addInput($dbName);
    	$form->addInput($dbHost);
    	$form->addInput($dbUser);
    	$form->addInput($dbPass);
    	$form->addInput($dbCharset);
    	$form->addInput($dbPort);
    	$form->addInput($backupDir);
	}
    
    /**
     * 个人用户的配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form) {}
    
}
